package com.demohomework.homeworkdemo.controller;

import com.demohomework.homeworkdemo.pojo.Course;
import com.demohomework.homeworkdemo.pojo.Result;
import com.demohomework.homeworkdemo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Validated
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    //新增
    @PostMapping("/add")
    public Result add(@RequestBody @Validated({Course.Add.class}) Course course) {
        courseService.add(course);
        return Result.success();
    }
    
    //课程信息
    @GetMapping("/list")
    public Result<List<Course>> list() {
        List<Course> courses = courseService.list();
        return Result.success(courses);
    }
    //根据id查询课程
    @GetMapping("/select/{id}")
    public Result<Course> show(@PathVariable("id") Integer id ) {
        Course c = courseService.findId(id);
        return Result.success(c);
    }
    //根据教师id查询课程
    @GetMapping("/{teacherId}")
    public Result<List<Course>> selectByTeacherId(@PathVariable("teacherId")Integer teacherId) {
        List<Course> courses = courseService.selectByTeacherId(teacherId);
        return Result.success(courses);
        }
    //更新课程
    @PutMapping("/update")
    public Result update(@RequestBody @Validated(Course.Update.class) Course course){
        courseService.update(course);
        return Result.success();
    }
    //删除课程
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")  Integer id){
        courseService.delete(id);
        return Result.success();
    }
}
